<?php

/*
 * 商品测试
 * @author wyb
 */

namespace Api\Api\Goods;

use Api\Api\Base;

class Goods extends Base {

    private $_model = null;

    public function __construct() {
        parent::__construct();
        $this->_model = M('Goods');
    }

    /**
     * 商品信息列表
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public function lists($data) {
        $sycn_data = M('customer_goods_sycn_log')->where(array('customer_id' => $data['app_id']))->getField('goods_id', true);
        $where = array();
        if ($sycn_data) {
            //$where['sku'] = array('NOT IN' , implode(',' , $sycn_data));
        }
        $where['gs.cusomter_id'] = $data['app_id'];
        $info = M('goods_supply')->alias('gs')->join('__GOODS__ g ON g.id = gs.goods_id')->join('LEFT JOIN __GOODS_BRAND__ gb ON gb.id = g.goods_brand_id')->where($where)->field('g.*,gb.name AS brand,gs.market_price,gs.stock_num,gs.lock_price,gs.lock_start_num,gs.wholesale_price,gs.wholesale_start_num,gs.acting_price,gs.price')->page($data['page'], $data['row'])->select();
        $total = M('goods_supply')->alias('gs')->join('__GOODS__ g ON g.id = gs.goods_id')->join('LEFT JOIN __GOODS_BRAND__ gb ON gb.id = g.goods_brand_id')->where($where)->count();
        if ($data['record'] == 1) {
            $record_data = array();
            foreach ($info as $key => $value) {
                $record_data[] = array(
                    'customer_id' => $data['app_id'],
                    'goods_id' => $value['id'],
                    'create_time' => time()
                );
            }
            M('customer_goods_sycn_log')->addAll($record_data);
        }
        return $info; //array('list' => $data , 'total' => $total);
    }

    /**
     * 库存查询
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public function get_product_rcount($data) {
        $sku_list_rcount = $this->_model->alias('g')->join('__GOODS_SUPPLY__ gs ON gs.goods_id = g.id')
                        ->where(array('gs.cusomter_id' => $data['app_id'], 'g.earn13' => array('IN', $data['earn13'])))
                        ->field('g.earn13,gs.stock_num AS rcount')->select();
        return $sku_list_rcount;
    }

    /**
     * 价格查询
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public function get_product_price($data) {
        $sku_list_price = $this->_model->alias('g')->join('__GOODS_SUPPLY__ gs ON gs.goods_id = g.id')
                        ->where(array('gs.cusomter_id' => $data['app_id'], 'g.earn13' => array('IN', $data['earn13'])))
                        ->field('gs.market_price,gs.lock_price,gs.acting_price,gs.wholesale_price,gs.price')->select();
        return $sku_list_price;
    }

    /**
     * 商品入库
     * @param  [type] $data [description]
     */
    public function goods_add($data) {
        //开启事物
        $this->_model->startTrans();
        //查询商品是否已经存在
        if ($goods_info = $this->_model->where(array('earn13' => $data['earn13']))->find()) {
            //商品存在则判断是否已经是商品的供应商
            if (M('goods_supply')->where(array('cusomter_id' => $data['app_id'], 'goods_id' => $goods_info['id']))->find()) {
                $this->_error = "商品【{$data['name']}】供应信息已存在，无需重复录入";
                return false;
            }
        } else {
            //商品信息入库
            $goods_add = array(
                'earn13' => $data['earn13'],
                'name' => $data['name'],
                'info_keywords' => $data['info_keywords'],
                'weight' => $data['weight'],
                'pom' => $data['pom'],
                'info_pc' => $data['info_pc'],
                'info_mobile' => $data['info_mobile'],
                'create_time' => time()
            );
            //商品信息入库
            if (!$goods_info['id'] = $this->_model->add($goods_add)) {
                $this->_error = '商品信息入库失败，请重新同步';
                $this->_model->rollback();
                return false;
            }
        }
        //添加供应信息
        $supply_add = array(
            'goods_id' => $goods_info['id'],
            'cusomter_id' => $data['app_id'],
            'market_price' => $data['market_price'],
            'cost_price' => $data['cost_price'],
            'price' => $data['price'],
            'stock_num' => $data['stock_num'],
            'warehouse_id' => $data['warehouse_id'],
            'create_time' => time()
        );
        if (M('goods_supply')->add($supply_add)) {
            $this->_model->commit();
            return true;
        }
        $this->_model->rollback();
        $this->_error = '商品供应信息入库失败，请重新同步';
        return false;
    }

    //商品库存更新
    function save_stock($data) {
        $goodInfo = M('goods')->where(array('earn13' => $data['earn13']))->find();
        if ($goodInfo) {
            $info = M('goods_supply')->where(array('cusomter_id' => $data['app_id'], 'goods_id' => $goodInfo['id']))->find();
            if ($info) {
                $d_stock = M('goods_distribution')->where(array('goods_supply_id' => $info['id']))->sum('stock_num');
                $tel['stock_num'] = $data['stock_num'] - $info['lock_stock_num'] - $d_stock; //可用库存 入货库存-锁货库存-分销库存
                $tel['stock_num_total'] = $data['stock_num'] + $info['sale_stock_num']; //供货中总数 入货库存+销售库存
                M('goods_supply')->where(array('cusomter_id' => $data['app_id'], 'goods_id' => $goodInfo['id']))->save($tel);
                $mod['content'] = '当前供货总数为:' . $tel['stock_num_total'] . "<br>锁货库存为：" . $info['lock_stock_num'] . "<br>分销库存为：" . ($d_stock ? $d_stock : 0) . "<br>销售库存为:" . $info['sale_stock_num'] . "<br>共享库存为:" . ($tel['stock_num_total'] - $info['sale_stock_num'] - $info['lock_stock_num']);
                $mod['goods_supply_product_id'] = $info['id'];
                M('goods_sync_stock_log')->add($mod);
                return true;
            } else {
                $this->_error = '库存更新失败，供货商商品不存在 ';
                return false;
            }
        } else {
            $this->_error = '库存更新失败，该商品不存在';
            return false;
        }
    }

}
